<?php


function logToFile($msg, $filename="test.log") {
	$fd = fopen("logs/".$filename, "a");
	$str = "[" . date("Y/m/d h:i:s", mktime()) . "] " . $msg;
	fwrite($fd, $str . "\n");
	fclose($fd);
}

function get_parent_id($service_id)
{

	global $adodbcon,$pid;
	
	
		$catListSql="SELECT * FROM ".TABLEPREFIX."_service WHERE  is_active='Y' AND service_id=".$service_id;
		$catList	= $adodbcon->Execute($catListSql);
		$catListArr	= $catList->FetchRow();

		$pid=$catListArr['service_id'];
	if($catListArr['service_parent_id']!=0)
	{
		
		
		get_parent_id($catListArr['service_parent_id']);
	
	
	
	}

		return $pid;
}



function serviceMenu($service_id,$current_service_id)
{

	global $adodbcon,$UserManagerObjAjax,$cteateserviceChk,$count_arr,$strBox;

	$catListSql="SELECT * FROM ".TABLEPREFIX."_service WHERE  is_active='Y' AND service_parent_id=".$service_id;
	$catListArr	= $UserManagerObjAjax->GetRecords("All",$catListSql);
	
	if($service_id==0)
	$count_arr=0;
	
	
	
	
	
	for($i=0;$i<count($catListArr);$i++)
	{
	
	
	$subcatListSql="SELECT * FROM ".TABLEPREFIX."_service WHERE  is_active='Y' AND service_parent_id=".$catListArr[$i]['service_id']."ORDER BY service_order";

	$subcatListArr	= $UserManagerObjAjax->GetRecords("All",$subcatListSql);
	
	
	if($current_service_id==$catListArr[$i]['service_id'])
	$chkBox='checked="checked"';
	else
	$chkBox='';
	
	
	
	
	if($i==0)
	$strBox.='<ul>';
	
	
	$strBox.='<li><a href="service.php?service_id='.$catListArr[$i]['service_id'].'">'.$catListArr[$i]['service_title'].'</a>';
	
	
	
	if(count($subcatListArr)==0)
	$strBox.='</li>';
	
	
	
		$count_arr++;
		serviceMenu($catListArr[$i]['service_id'],$current_service_id);
	
	
		if(count($catListArr)==($i+1))
		$strBox.='</ul></li>';
	
	}
	
	
	return $strBox;

}

function cteateserviceChk($service_id,$current_service_id)
{

	global $adodbcon,$UserManagerObjAjax,$cteateserviceChk,$count_arr,$strBox;

	$catListSql="SELECT * FROM ".TABLEPREFIX."_service WHERE  is_active='Y' AND service_parent_id=".$service_id;
	$catListArr	= $UserManagerObjAjax->GetRecords("All",$catListSql);
	
	if($service_id==0)
	$count_arr=0;
	
	for($i=0;$i<count($catListArr);$i++)
	{
	
	$subcatListSql="SELECT * FROM ".TABLEPREFIX."_service WHERE  is_active='Y' AND service_parent_id=".$catListArr[$i]['service_id'];

	$subcatListArr	= $UserManagerObjAjax->GetRecords("All",$subcatListSql);
	
	
	if($current_service_id==$catListArr[$i]['service_id'])
	$chkBox='checked="checked"';
	else
	$chkBox='';
	
	if($i==0)
	$strBox.='<ul>';
	
	$strBox.='<li><input class="check" type="checkbox"  name="chk_'.$catListArr[$i]['service_id'].'"  id="chk_'.$catListArr[$i]['service_id'].'" value="'.$catListArr[$i]['service_title'].'" '.$chkBox.'>'.$catListArr[$i]['service_title'];
	
	if(count($subcatListArr)==0)
	$strBox.='</li>';
	
	//$cteateserviceChk[$count_arr]='<input type="checkbox" id="'.$catListArr[$i]['service_id'].'" value="'.$catListArr[$i]['service_title'].'" '.$chkBox.'>'.$catListArr[$i]['service_title'];
	
		$count_arr++;
		cteateserviceChk($catListArr[$i]['service_id'],$current_service_id);
	
		if(count($catListArr)==($i+1))
		$strBox.='</ul></li>';
	
	}
	
	
	return $strBox;

}




function getList($tbl_name,$type,$title_name,$catKey=0)
{

	global $adodbcon,$UserManagerObjAjax,$bcatLinkArr,$count_arr;
	


	$ListSql="SELECT * FROM ".TABLEPREFIX."_".$tbl_name."
						WHERE  is_active='Y' ORDER BY ".$tbl_name."_order";
						
	$ListArr=$UserManagerObjAjax->GetRecords("All",$ListSql);
	
	
	
	
	for($i=0;$i<count($ListArr);$i++)
	{
	

		if($ListArr[$i][$tbl_name.'_id']==$catKey)
		{
			$storng_start='<strong>';
			$storng_end='</strong>';			
		
		}
		else
		{
		
			$storng_start='';
			$storng_end='';			
		}



		$finalLinkArr[$i]['samelevel']='<a href="'.$tbl_name.'.php?'.$tbl_name.'_id='.$ListArr[$i][$tbl_name.'_id'].'">'.$storng_start.$ListArr[$i][$title_name].$storng_end.'</a>';
	
	
	}

	
	
	
	return $finalLinkArr;


}






function getLink($service_id)
{
	global $adodbcon,$UserManagerObjAjax,$bcatLinkArr,$count_arr;

	$catListSql="SELECT * FROM ".TABLEPREFIX."_service WHERE  is_active='Y' AND service_id=".$service_id." ORDER BY service_order";

	$catList	= $adodbcon->Execute($catListSql);
	$catListArr	= $catList->FetchRow();
	
	$count_arr++;
	
	$bcatLinkArr[$count_arr]['linkTitle']=$catListArr['service_title'];
	$bcatLinkArr[$count_arr]['id']=$catListArr['service_id'];
	
	if($catListArr['service_parent_id']!=0)	
	getLink($catListArr['service_parent_id']);
	
	
	
	
	return $bcatLinkArr;


}




function leftmenu($tbl_name,$pkey,$service_id)
{

	global $adodbcon,$UserManagerObjAjax;

	
	$service_parent_id=$pkey;
	
	
	$catListSql="SELECT * FROM ".TABLEPREFIX."_".$tbl_name."
						WHERE  is_active='Y' AND ".$tbl_name."_parent_id=".$service_parent_id." ORDER BY service_order";
						
	$catListArr=$UserManagerObjAjax->GetRecords("All",$catListSql);
	
	
	for($i=0;$i<count($catListArr);$i++)
	{
	
		
	if($service_id==$catListArr[$i]['service_id'])
	{
		
		$class='class="active-left"';
			
	}
	else
	{
	
		$class='';
	
	}

		$finalLinkArr[$i]['samelevel']='<li><a href="service.php?service_id='.$catListArr[$i]['service_id'].'" '.$class.'>'.$catListArr[$i]['menu_title'].'</a></li>';
	
	
	}

	return $finalLinkArr;
	
	
}




function getcatLink($tbl_name,$pkey,$type)
{

	global $adodbcon,$UserManagerObjAjax;




	
	
	
	
	
	if($type=='menuLeft')
	{
	
	$service_parent_id=$pkey;
	
	
	 $catListSql="SELECT * FROM ".TABLEPREFIX."_".$tbl_name."
						WHERE  is_active='Y' AND ".$tbl_name."_parent_id=".$service_parent_id." ORDER BY service_order";
						
	$catListArr=$UserManagerObjAjax->GetRecords("All",$catListSql);
	
	
	for($i=0;$i<count($catListArr);$i++)
	{
	
		$finalLinkArr[$i]['samelevel']='<li><a href="service.php?service_id='.$catListArr[$i]['service_id'].'">'.$catListArr[$i]['service_title'].'<sup>TM</sup><br /></a></li>';
	
	
	}
	
	return $finalLinkArr;
	
	
	}
	
	
	
	
	
	
	
	
	
	
	
	


	
	if($type=='onelevel')
	{
	
	$service_parent_id=$pkey;
	
	
	$catListSql="SELECT * FROM ".TABLEPREFIX."_".$tbl_name."
						WHERE  is_active='Y' AND ".$tbl_name."_parent_id=".$service_parent_id." ORDER BY service_order";
						
	$catListArr=$UserManagerObjAjax->GetRecords("All",$catListSql);
	
	
	for($i=0;$i<count($catListArr);$i++)
	{
	
$finalLinkArr[$i]['samelevel']='<h2><a href="service.php?service_id='.$catListArr[$i]['service_id'].'">'.$catListArr[$i]['service_title'].'</a></h2>';
	
	
	}

	return $finalLinkArr;
	
	
	}

	if($type=='twolevel')
	{
	
	echo TABLEPREFIX."_".$tbl_name;
	
	echo $catListSql="SELECT * FROM ".TABLEPREFIX."_".$tbl_name."
						WHERE  is_active='Y' AND ".$tbl_name."_id=".$pkey." ORDER BY service_order";
						
	$catListArr=$UserManagerObjAjax->GetRecords("All",$catListSql);
	
	for($i=0;$i<count($catListArr);$i++)
	{
	
		//$finalArr[$i]['samelevel']='';
		echo $catListArr[$i]['is_one_level_submenu'];
		if($catListArr[$i]['is_one_level_submenu']=='Y')
		{
		
			$subCatListSql="SELECT * FROM ".TABLEPREFIX."_".$tbl_name."
							WHERE  is_active='Y' AND ".$tbl_name."_parent_id=".$catListArr[$i]['service_id']." ORDER BY service_order";	
			$subCatListArr=$UserManagerObjAjax->GetRecords("All",$subCatListSql);
			
			for($j=0;$j<count($subCatListArr);$j++)
			{
			
				if($subCatListArr[$j]['linkable']=='Y')
				$finalArr[$i]['nextlevel'][$j]='<li><a href="service.php?service_id='.$subCatListArr[$j]['service_id'].'">'.$subCatListArr[$j]['service_title'].'</a></li>';	
				else
				$finalArr[$i]['nextlevel'][$j]='<li>'.$subCatListArr[$j]['service_title'].'</li>';	
			
			}
		
		}
	
	}
	
			return $finalArr;
	
	
	}

}

function getMetaContent($tbl_name,$pkey)
{


	
	
	global $adodbcon,$UserManagerObjAjax;




	$pgSQL	= "SELECT * FROM ". TABLEPREFIX ."_".$tbl_name." WHERE `".$tbl_name."_id` = ". $pkey;
	$pgLink	= $adodbcon->Execute($pgSQL);
	$pg_info	= $pgLink->FetchRow();
	
	
	
	
	
	
	$pageInfo['cms_title']=$pg_info[$tbl_name.'_title'];	
	$pageInfo['content']=$pg_info[$tbl_name.'_describtion'];	
	$pageInfo['metatag_title']=$pg_info['metatag_title'];	
	$pageInfo['metatag_keywords']=$pg_info['metatag_keywords'];
	$pageInfo['metatag_description']=$pg_info['metatag_description'];


	
	return $pageInfo;


}





function createLinkBlock($tbl_name,$pkey_name,$pkey_value)
{

global $adodbcon,$UserManagerObjAjax;




	$GrpLinkSql="SELECT * FROM ".TABLEPREFIX."_".$tbl_name."_link_group 
						WHERE ".$pkey_name."=".$pkey_value;
						
	$GrpLinkArr=$UserManagerObjAjax->GetRecords("All",$GrpLinkSql);
	
	
	for($i=0;$i<count($GrpLinkArr);$i++)
	{
	
				$grp_title_col=$tbl_name.'_group_title';
				$grp_title_key=$tbl_name.'_group_id';
				
				$finalLinkArr[$i]['link_grp_title']=$GrpLinkArr[$i][$grp_title_col];
				$LinkSql="SELECT * FROM ".TABLEPREFIX."_".$tbl_name."_link
									WHERE  is_active='Y' AND ".$tbl_name."_group_id=".$GrpLinkArr[$i][$grp_title_key];
									
				$LinkArr=$UserManagerObjAjax->GetRecords("All",$LinkSql);
				
				
				$link_col=$tbl_name.'_link';
				$link_open_col=$tbl_name.'_link_open_type';
				$link_title_col=$tbl_name.'_link_title';				
				
				
				for($j=0;$j<count($LinkArr);$j++)
				{
					
					
				$LinkArr[$j][$link_col]=str_replace("http://", "", $LinkArr[$j][$link_col]);				
				$finalLinkArr[$i]['link'][$j]='<li><a href="http://'.$LinkArr[$j][$link_col].'" target="'.$LinkArr[$j][$link_open_col].'">'.$LinkArr[$j][$link_title_col].'</a></li>';;

				}
	} 
	return $finalLinkArr;
}

function getUserStatus($user){ 
	global $adodbcon,$UserManagerObjAjax;
	$last_ping_time = $user['last_ping_time'];
	if($last_ping_time == null || $last_ping_time == "" || $user['is_logged_in'] == '0'){
		return 'offline';
	}
	
	$query = "SELECT SYSDATE() as now";
	$today = $UserManagerObjAjax->GetRecords("Row",$query);
// 	logToFile("XXX: ".$today['now']);
// 	print_r($today);

	
	$last_ping_time_seconds = intval(strtotime($last_ping_time));  
// 	$today = getdate();
// 	$current_second = intval($today[0]);
	$current_second = intval(strtotime($today['now']));
	
	
	$differenceInSecconds = $current_second - $last_ping_time_seconds;
	
	if($differenceInSecconds>180){
		return 'offline';
	}

	switch ($user['online_status']) {
		case "0":
			return 'offline';	
			break;
		case "1":
			return 'online';	
			break;
		case "2":
			return 'away';	
			break;
		case "3":
			return 'busy';	
			break;
		default:
			return 'offline';	
	}
}
?>